﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace HPPClientLibrary
{
    class ShareFilesXO : IXmlable
    {
        private List<ShareFileItem> _itemList;
        private int _port;

        public int Port
        {
            get { return _port; }
        }

        public ShareFilesXO(int port, List<ShareFileItem> itemList)
        {
            _port = port;
            _itemList = itemList;
        }

        public XDocument ToXml()
        {
            XDocument doc = new XDocument();
            doc.Declaration = new XDeclaration("1.0", "utf-8", "yes");
            XElement shareFiles = new XElement("ShareFiles");
            XElement info = new XElement("Info");
            foreach (var item in _itemList)
            {
                try
                {
                    XElement file = new XElement("File");
                    FileInfo fi = new FileInfo(item.FileFullPath);
                    file.SetAttributeValue("FileName", fi.Name);
                    file.SetAttributeValue("Hash", item.Hash);
                    file.SetAttributeValue("Size", fi.Length);
                    shareFiles.Add(file);
                }
                catch (FileNotFoundException ex)
                {
                    CommonLogger.Logger.Info("ShareFilesXO", ex);
                    
                }

            }

            XElement client = new XElement("Client");
            client.SetAttributeValue("Port", Port);

            info.Add(client);
            info.Add(shareFiles);
            doc.Add(info);

            return doc;
        }
        /*
        public ShareFilesXO FromXML(string xml)
        {
            
            try
            {
                XDocument doc = XDocument.Parse(xml);

                this._itemList = new List<ShareFileItem>();
                var files = from f in doc.Descendants("File")
                            select new { fileName = f.Attribute("FileName").Value, hash = f.Attribute("Hash").Value, fileSize = f.Attribute("Size") };

                foreach (var element in doc.Descendants("File"))
                {
                    try
                    {
                        string fileName = element.Attribute("FileName") == null ? null : element.Attribute("FileName").Value;
                        string hash = element.Attribute("Hash") == null ? null : element.Attribute("Hash").Value;
                        long? len = element.Attribute("Size") == null ? (long?) null : long.Parse(element.Attribute("Hash").Value);

                        if (fileName == null || hash == null || len == null)
                        {
                            continue;
                            
                        }

                        ShareFileItem item = new ShareFileItem();
                    }
                    catch (Exception)
                    {
                        
                        throw;
                    }
                    
                }
            }
            catch (Exception e)
            {
                CommonLogger.Logger.Error("ShareFilesXO", e);
            }



            return null;
             
        }
        */
        
    }
}
